home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / BOUNCE_C / BOUNCE.C next >
Text File  |  1991-01-25  |  4KB  |  191 lines

  1. /*
  2.  * bounce.c -- Bounce's main code section.
  3.  *
  4.  * Bounce is a cdev that displays a bouncing line in the control panel.  When you
  5.  * select the about button, it puts up a dialog that also contains a bouncing line,
  6.  * using the same code for drawing both lines.
  7.  *
  8.  * This illustrates that cdevs are re-entrant, i.e. an executing cdev can indirectly
  9.  * call itself by using Alert, ModalDialog, SFGetFile, or any other function which
  10.  * has an event loop.  Also, this cdev does its work during null event time, so it
  11.  * will continue to draw in a background MultiFinder layer.
  12.  *
  13.  * Since the about box code uses SetUpA4, it isn't truly re-entrant, because SetUpA4
  14.  * only contains one copy of A4.  This doesn't affect us, since the about box code
  15.  * itself isn't called re-entrantly, and A4 would be the same in any case (using
  16.  * current system software, at least!).
  17.  * by Phil Shapiro.  Copyright ⌐ 1990,1991 Symantec Corp. All rights reserved.
  18.  */
  19. #include "bounce.h"
  20. /*
  21.  * This useful library contains a function which centers a dialog box on the
  22.  * screen that contains the mouse.  It's used for the about box.
  23.  */
  24. #include "DialogUtils.h"
  25.  
  26. /*
  27.  *  Runnable - should the cdev appear in the Control Panel?
  28.  *
  29.  *  This implements the "macDev" message.
  30.  *
  31.  */
  32. Boolean Runnable()
  33. {
  34.     return(true);
  35. }
  36.  
  37. /*
  38.  *  New - create the cdev object.
  39.  */
  40. cdev * New()
  41. {
  42.     return((cdev *)new(Bounce));
  43. }
  44.  
  45. /*
  46.  * If you don't call the inherited method, you *MUST* return 'this' or you will
  47.  * die the next time the cdev is called.
  48.  */
  49. long Bounce::Message(int msg, int item)
  50. {
  51.     if (msg == cursorDev)
  52.         Cursor();
  53.     return inherited::Message(msg, item);
  54. }
  55.  
  56. /*
  57.  * Initialize the cdev.  First load in my cursor, then allocate the LineBox.
  58.  */
  59. void Bounce::Init()
  60. {
  61.     int type;
  62.     Rect box;
  63.     Handle hdl;
  64.  
  65.     cursor = ReadRsrc('CURS', CURSID);
  66.     if (lines = new(LineBox)) {
  67.         GetDItem((DialogPtr)dp, lastItem + BounceItem, &type, &hdl, &box);
  68.         if (lines->Init(NUM_LINES, &box, (GrafPtr)dp)) {
  69.             inherited::Init();
  70.             return;
  71.         }
  72.         delete(lines);
  73.     }
  74.     else
  75.         Error(cdevMemErr);
  76. }
  77.  
  78. /*
  79.  * Cleanup by closing the lines and deleting it.
  80.  */
  81. void Bounce::Close()
  82. {
  83.     lines->Close();
  84.     delete(lines);
  85.     inherited::Close();
  86. }
  87.  
  88. /*
  89.  * Just tell the lines to draw itself.
  90.  */
  91. void Bounce::Update()
  92. {
  93.     lines->Draw();
  94.     inherited::Update();
  95. }
  96.  
  97. /*
  98.  * The only item which does anything is the '?' button.  This brings up the
  99.  * about box.
  100.  */
  101. void Bounce::ItemHit(int item)
  102. {
  103.     Point where = event->where;
  104.  
  105.     switch (item) {
  106.     case TitleItem:
  107.         break;
  108.     case AboutItem:
  109.         About();
  110.         break;
  111.     case BounceItem:
  112.         break;
  113.     default:
  114.         inherited::ItemHit(item);
  115.     }
  116. }
  117.  
  118. /*
  119.  * During idle time, call the lines Idle routine.  This animates the line.
  120.  */
  121. void Bounce::Idle()
  122. {
  123.     lines->Idle();
  124.     inherited::Idle();
  125. }
  126.  
  127. /*
  128.  * This will let my cdev use the arrow cursor.  It's stored in a resource since cdevs
  129.  * have no qd globals.
  130.  */
  131. void Bounce::Cursor()
  132. {
  133.     if (!cursor)
  134.         return;
  135.     SetCursor((Cursor *)*cursor);
  136. }
  137.  
  138. /*
  139.  * This routine prepares to bring up the about box.  Typically this would be an ALRT,
  140.  * but I need the bounding box for the AboutBounceItem, and there's no (easy) way to
  141.  * get at that information from an ALRT's DITL.  Also, it's harder to preload and
  142.  * set the refCon of an ALRT than a DLOG.  The refCon is used to pass the aboutLines
  143.  * object to the dialog's filter proc.  The type and box args are used by BounceAbout
  144.  * to set the UserItem's draw procedure.
  145.  */
  146. void Bounce::About()
  147. {
  148.     LineBox *aboutLines;
  149.     DialogPtr aboutBox;
  150.     int type;
  151.     Rect box;
  152.     Handle hdl;
  153.     int itemHit;
  154.  
  155.     aboutBox = (DialogPtr) GetNewDialog(DU_CenterDLOG(AboutID), NIL, (WindowPtr)-1);
  156.     if (!aboutBox)
  157.         return;
  158.     /* If allocating the LineBox fails, we skip the about box. */
  159.     if (aboutLines = new(LineBox)) {
  160.         GetDItem(aboutBox, AboutBounceItem, &type, &hdl, &box);
  161.         if (aboutLines->Init(NUM_LINES, &box, (GrafPtr)aboutBox)) {
  162.             SetWRefCon((WindowPtr) aboutBox, (long) aboutLines);
  163.             BounceAbout(aboutBox, type, &box);
  164.             aboutLines->Close();
  165.         }
  166.         delete(aboutLines);
  167.     }
  168.     DisposDialog(aboutBox);
  169. }
  170.  
  171. /*
  172.  * ReadRsrc -- read a resource from the cdev's file and set the error codes
  173.  * if anything goes wrong.  Otherwise, set the non-purge bit.
  174.  */
  175. Handle Bounce::ReadRsrc(OSType type, int number)
  176. {
  177.     Handle rsrc;
  178.     int refNum;
  179.  
  180.     rsrc = Get1Resource(type, number);
  181.     if (!rsrc || (ResError() != noErr)) {
  182.         Error(cdevResErr);
  183.         if (rsrc)
  184.             ReleaseResource(rsrc);
  185.         return NIL;
  186.     }
  187.     HNoPurge(rsrc);
  188.  
  189.     return rsrc;
  190. }
  191.